/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dados;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

/**
 *
 * @author emanoel
 */
public class ValidadorXml {    

    public static boolean validar(String caminhoXsd, String caminhoXml) throws ParserConfigurationException, IOException {
        try{
           // 1. Lookup a factory for the W3C XML Schema language
        SchemaFactory factory = 
            SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        
        // 2. Compile the schema. 
        // Here the schema is loaded from a java.io.File, but you could use 
        // a java.net.URL or a javax.xml.transform.Source instead.
        File schemaLocation = new File(caminhoXsd);
        Schema schema = factory.newSchema(schemaLocation);
    
        // 3. Get a validator from the schema.
        Validator validator = schema.newValidator();
        
        // 4. Parse the document you want to check.
        Source source = new StreamSource(caminhoXml);
        
        // 5. Check the document
        validator.validate(source);
        return true;
        } catch(SAXException ex){
            return false;
        }
    }
}
